const fs = require('fs');
const path = require('path');

// 定义需要替换的映射关系
const staticsReplacements = {
  // 图片资源映射
  'statics.unicardProduct': "'#FFFFFF'",
  'statics.unicardInfoBg': "'#F0F0F0'",
  'statics.tuanTodayImg': "'#FF4444'",
  'statics.unicardBg': "'#5FB878'",
  'statics.exchangeBg': "'#9D65C9'",
  'statics.signPlusBg': "'#4ECDC4'",
  'statics.signBox': "'#4ECDC4'",
  'statics.signResult': "'#4ECDC4'",
  'statics.plusBg': "'#FFB800'",
  'statics.couponBg': "'#FF6B6B'",
  'statics.couponFullBg': "'#FF6B6B'",
  'statics.couponActivityImg': "'#FF6B6B'",
  'statics.noImg': "''",
  
  // 图标映射
  'statics.signAward': "'icon_diamond'",
  'statics.signRole': "'iconbtn_question'",
  'statics.signBu': "'iconbtn_add'",
  'statics.signYes': "'icon_diamond'",
  'statics.invitePlus': "'icon_plus'",
  'statics.inviteStore': "'iconbtn_bottom_shop'",
  'statics.integral': "'icon_diamond'",
  
  // 其他资源设置为空字符串
  'statics.storeLogo01': "''",
  'statics.storeLogo02': "''",
  'statics.product01': "''",
  'statics.product02': "''",
  'statics.product03': "''",
  'statics.memberFace': "''",
  'statics.faceDefault': "''",
  'statics.closeImg': "''",
  'statics.qrcode': "''",
  'statics.couponNew': "''",
  'statics.couponPlus': "''",
  'statics.emptyCoupon': "''",
  'statics.emptyMoney': "''",
  'statics.emptyOrder': "''",
  'statics.couponCommon': "''",
  'statics.couponDetailLine': "''",
  'statics.bannerPic01': "''",
  'statics.bannerPic02': "'#F0F0F0'",
  'statics.toutiaoImg': "''",
  'statics.buyBgImg': "''",
  'statics.noticeImg': "''",
  'statics.recPic02': "''",
  'statics.tagHot': "''",
  'statics.plusTui': "''",
  'statics.moneyIcon': "''",
  'statics.rechargeIcon': "''",
  'statics.yesImg': "''",
  'statics.signLotteryL': "''",
  'statics.signLotteryR': "''",
  'statics.signImg': "''",
  'statics.signResultBg': "''",
  'statics.couponAlertImg': "''",
  'statics.unicardRoleCoupon': "''",
  'statics.unicardRoleTuan': "''",
  'statics.signAlertImg': "''",
  'statics.buyAlertImg': "''",
  'statics.plusIntegralRole': "''",
  'statics.integralExchange': "''",
  'statics.integralIcons': "[]"
};

// 遍历目录下的所有.vue文件
function traverseDirectory(dir) {
  // 跳过node_modules目录
  if (dir.includes('node_modules')) {
    return;
  }
  
  try {
    const files = fs.readdirSync(dir);
    files.forEach(file => {
      const filePath = path.join(dir, file);
      
      try {
        const stat = fs.statSync(filePath);
        
        if (stat.isDirectory()) {
          traverseDirectory(filePath);
        } else if (file.endsWith('.vue')) {
          replaceStaticsInFile(filePath);
        }
      } catch (err) {
        console.log(`跳过无法访问的文件: ${filePath}`);
      }
    });
  } catch (err) {
    console.log(`跳过无法访问的目录: ${dir}`);
  }
}

// 在文件中替换statics调用
function replaceStaticsInFile(filePath) {
  try {
    let content = fs.readFileSync(filePath, 'utf8');
    let hasChanges = false;
    
    // 替换computed中的background-color模板字符串
    content = content.replace(/return\s*['"`]background-color:(.+?)['"`]\s*[+;]/g, (match, p1) => {
      const staticsRef = p1.trim();
      if (staticsReplacements[staticsRef]) {
        hasChanges = true;
        const colorValue = staticsReplacements[staticsRef].replace(/['"]/g, '');
        return `return 'background-color:${colorValue};';`;
      }
      return match;
    });
    
    // 替换computed中的background:url模板字符串
    content = content.replace(/return\s*['"`]background\:url\((.+?)\)/g, (match, p1) => {
      const staticsRef = p1.trim();
      if (staticsReplacements[staticsRef]) {
        hasChanges = true;
        const bgValue = staticsReplacements[staticsRef].replace(/['"]/g, '');
        return `return 'background:url(${bgValue})'`;
      }
      return match;
    });
    
    // 替换:src中的statics调用
    Object.keys(staticsReplacements).forEach(key => {
      const regex = new RegExp(`:src=\"${key}\"`, 'g');
      if (regex.test(content)) {
        hasChanges = true;
        content = content.replace(regex, `:src="${staticsReplacements[key]}"`);
      }
    });
    
    // 替换直接使用的this.statics引用
    Object.keys(staticsReplacements).forEach(key => {
      const thisKey = key.replace('statics.', 'this.statics.');
      // 使用更安全的方式构建正则表达式
      const plusPattern = '\\+' + '\\s*' + thisKey + '\\s*' + '\\+';
      const regex = new RegExp(plusPattern, 'g');
      if (regex.test(content)) {
        hasChanges = true;
        const value = staticsReplacements[key];
        content = content.replace(regex, '+ ' + value + ' +');
      }
    });
    
    if (hasChanges) {
      fs.writeFileSync(filePath, content, 'utf8');
      console.log(`已更新文件: ${filePath}`);
    }
  } catch (error) {
    console.error(`处理文件时出错 ${filePath}: ${error.message}`);
  }
}

// 开始执行替换
const projectRoot = process.argv[2] || '.';
traverseDirectory(projectRoot);
console.log('所有文件替换完成！');